<template>
  <div class="params">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>888888</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon :closable="false"> </el-alert>

      <div class="block">
        <span class="demonstration">选择商品分类：</span>
        <el-cascader v-model="value" :options="options" :props="{ expandTrigger: 'hover', value: 'cat_id', label: 'cat_name', children: 'children' }" @change="handleChange"></el-cascader>

        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="动态参数" name="many">
            <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加参数</el-button>
            <el-table :data="manyTableData" style="width: 100%" border stripe>
              <el-table-column type="expand">
                <!-- tag-->
                 <template slot-scope="scope">
                  <el-tag v-for="(tag,i) in scope.row.attr_vals" :key="i"> {{ tag }} </el-tag>
                  <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
                  </el-input>
                  <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
                  <!-- tag-->
                </template>
              </el-table-column>
              <el-table-column type="index"> </el-table-column>
              <el-table-column prop="attr_name" label="参数名称" width="180"> </el-table-column>
              <el-table-column prop="" label="操作" width="180">
                <template slot-scope="scope">{{ scope.row.attr_id }}</template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="静态参数" name="only">
            <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加属性</el-button>
            <el-table :data="onlyTableData" style="width: 100%" border stripe>
              <el-table-column type="expand">
                <!-- tag-->
                <template slot-scope="scope">
                  <el-tag> {{ scope.row.attr_vals }} </el-tag>

                  <!-- tag-->
                </template>
              </el-table-column>
              <el-table-column type="index"> </el-table-column>
              <el-table-column prop="attr_name" label="参数名称" width="180"> </el-table-column>
              <el-table-column prop="" label="操作" width="180">
                <template slot-scope="scope">{{ scope.row.attr_id }}</template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>

      <!-- 添加参数的对话框 -->
      <el-dialog :title="titleText" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
        <!-- 添加参数的对话框 -->
        <el-form :model="addForm" ref="addFormRef" label-width="100px">
          <el-form-item :label="titleText" prop="attr_name">
            <el-input v-model="addForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addParams">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //级联选择器
      value: [],
      options: [],
      // 页签
      activeName: 'many',
      manyTableData: [],
      onlyTableData: [],
      //tag
      dynamicTags: [],
      inputVisible: false,
      inputValue: [],
      //dialog
      addDialogVisible: false,
      // 添加参数的表单数据对象
      addForm: {
        attr_name: ''
      }
    }
  },
  methods: {
    handleChange(value) {
      console.log(value)
      this.getCategoriesList()
    },
    //获取商品列表
    getOptionsList() {
      this.$http
        .get('categories')
        .then(result => {
          if (result.data.meta.status !== 200) {
            return this.$message.error('获取列表失败！')
          } else {
            this.options = result.data.data
          }
        })
        .catch(err => {
          console.log(err)
        })
      //点击
    },
    //获取某商品参数
    getCategoriesList() {
      this.$http
        .get(`categories/${this.cateId}/attributes`, {
          params: { sel: this.activeName }
        })
        .then(result => {
          console.log(result.data)
          if (result.data.meta.status !== 200) {
            return this.$message.error('获取列表失败！')
          } else {
            console.log(result.data)
            result.data.data.forEach(item => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
            })

   

            console.log(this.dynamicTags)
            if (this.activeName === 'many') {
              this.manyTableData = result.data.data
            } else {
              this.onlyTableData = result.data.data
            }
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    handleClick() {
      //console.log(tab)
      console.log(this.activeName)

      this.getCategoriesList()
    },
    /**添加参数 */
    // 监听添加对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },

    // 点击按钮，添加参数
    addParams() {
      this.$http
        .post(`categories/${this.cateId}/attributes`, {
          attr_name: this.addForm.attr_name,
          attr_sel: this.activeName
        })
        .then(res => {
          if (res.data.meta.status !== 201) {
            return this.$message.error('添加参数失败！')
          }

          this.$message.success('添加参数成功！')
          this.addDialogVisible = false
          this.getParamsData()
        })
    },
    /*** tag*/
    //关闭
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    //点击
    showInput() {
      this.inputVisible = true
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      let inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  },
  computed: {
    // 当前选中的三级分类的Id
    cateId() {
      if (this.value.length === 3) {
        return this.value[2]
      }
      return null
    },
    // 如果按钮需要被禁用，则返回true，否则返回false
    isBtnDisabled() {
      if (this.value.length !== 3) {
        return true
      }
      return false
    },
    titleText() {
      if (this.activeName === 'many') {
        return '添加动态参数'
      }
      return '添加静态属性'
    }
  },
  mounted() {
    this.getOptionsList()
  }
}
</script>
<style lang="less" scoped></style>
